//
//  WaterFallViewController.m
//  AFNetworking
//
//  Created by admin on 2020/4/20.
//

#import "WaterFallViewController.h"
#import "UGCollectionViewWaterFallFlowLayout.h"
#import "DefualCollectionViewCell.h"

@interface WaterFallViewController ()<UICollectionViewDataSource, UGCollectionViewWaterFallFlowDelegate>

@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong)UGCollectionViewWaterFallFlowLayout * flowLayout;
@end

@implementation WaterFallViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = @"瀑布流";
    self.view.backgroundColor = UIColor.whiteColor;
        [self configUI];
}

-(void)configUI{
      [self.view addSubview:self.collectionView];
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        [_collectionView registerClass:[DefualCollectionViewCell class] forCellWithReuseIdentifier:@"DefualCollectionViewCell"];
    }
    return _collectionView;
}
-(UGCollectionViewWaterFallFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [UGCollectionViewWaterFallFlowLayout new];
        _flowLayout.delegate = self;
    }
    return _flowLayout;
}

#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 100;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DefualCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DefualCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = UIColor.ug_random;
    cell.titleLab.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    [cell ug_radius:4];
    return cell;
}


#pragma mark  - <LMHWaterFallLayoutDeleaget>
- (CGFloat)waterFallLayout:(UGCollectionViewWaterFallFlowLayout *)waterFallLayout heightForItemAtIndexPath:(NSUInteger)indexPath itemWidth:(CGFloat)itemWidth{
  
    return random()%40+180;
}

/**
* 每行之间的间距
*/
- (CGFloat)rowMarginInWaterFallLayout:(UGCollectionViewWaterFallFlowLayout *)waterFallLayout{
    
    return 2;
}
/**
 * 每列之间的间距
 */
- (CGFloat)columnMarginInWaterFallLayout:(UGCollectionViewWaterFallFlowLayout *)waterFallLayout{
    return 2;
}

/**
* 有多少列
*/
- (NSUInteger)columnCountInWaterFallLayout:(UGCollectionViewWaterFallFlowLayout *)waterFallLayout{
    
    return 2;
    
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}


@end
